package com.work.lxykeeper.module.main;

import android.annotation.TargetApi;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowInsets;

import com.work.lxykeeper.cache.Ckey;
import com.work.lxykeeper.cache.SPutils;
import com.work.lxykeeper.module.pagemine.LoginActivity;
import com.work.lxykeeper.utils.LogUtils;
import com.work.lxykeeper.utils.TelManagerUtils;
import com.work.lxykeeper.utils.ToastUtils;
import com.work.lxykeeper.utils.UIUtils;
import com.work.lxykeeper.view.PrivacyPolicyDialog;


/**
 * author：WangZhan
 * data: 2017/1/14 09:00
 * version: 1.0.0
 * explain:闪屏界面
 */

public class SplashActivity extends AppCompatActivity {
    private Intent intent;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            String isfirst = SPutils.get(Ckey.ISFIRST);
            if (TextUtils.isEmpty(isfirst)) {
                //第一次启动进入引导页
//                intent = new Intent(SplashActivity.this, GuideActivity.class);
                //第一次加载app显示隐私政策
                PrivacyPolicyDialog.startDialog(SplashActivity.this, new PrivacyPolicyDialog.CallbackAgree() {
                    @Override
                    public void agreen() {
                        //同意隐私政策
                        LogUtils.d("同意了");
                        String Str1 = SPutils.get(Ckey.UUID);
                        if (TextUtils.isEmpty(Str1)) {
                            Str1 = TelManagerUtils.getPhoneSign(UIUtils.getContext());
                            SPutils.put(Ckey.UUID, Str1);
                        }
                        SPutils.put(Ckey.ISFIRST, "false");

                        intent = new Intent(SplashActivity.this, LoginActivity.class);
//                        intent = new Intent(SplashActivity.this, GuideActivity.class);
                        UIUtils.startActivity(intent);
                        finish();
                    }

                    @Override
                    public void unagreen() {
                        //不同意隐私政策
                        LogUtils.d("没同意");
                    }
                });
            } else {
                if (!TextUtils.isEmpty(SPutils.get(Ckey.TOKEN))) {
                    //已经登录直接跳到主页面
                    intent = new Intent(SplashActivity.this, MainActivity.class);
//                    intent = new Intent(SplashActivity.this, DragListViewActivity.class);
                } else {
                    //进入登录页面
                    intent = new Intent(SplashActivity.this, LoginActivity.class);
                }
                UIUtils.startActivity(intent);
                finish();
            }
            handler.removeCallbacksAndMessages(null);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_splash);
        initSplashThem();
        handler.sendEmptyMessageDelayed(0, 1500);
        ToastUtils.cancel();
    }

    private void initSplashThem() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            View decorView = window.getDecorView();
            decorView.setOnApplyWindowInsetsListener(new View.OnApplyWindowInsetsListener() {
                @TargetApi(Build.VERSION_CODES.KITKAT_WATCH)
                @Override
                public WindowInsets onApplyWindowInsets(View v, WindowInsets insets) {
                    WindowInsets defaultInsets = v.onApplyWindowInsets(insets);
                    return defaultInsets.replaceSystemWindowInsets(
                            defaultInsets.getSystemWindowInsetLeft(),
                            0,
                            defaultInsets.getSystemWindowInsetRight(),
                            defaultInsets.getSystemWindowInsetBottom());
                }
            });
            ViewCompat.requestApplyInsets(decorView);
            //将状态栏设成透明，如不想透明可设置其他颜色
            window.setStatusBarColor(ContextCompat.getColor(this, android.R.color.transparent));
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 在欢迎界面屏蔽BACK键
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            return false;
        }
        return false;
    }
}
